<template>
    <div class="phonebackbord">
        <p class="title">{{Title}}</p>
        <p class="Uid">Uid：{{Uid}}</p>
        <p class="detail">{{detail}}</p>
        <div class="btn_group">
            <Button type="success" long style="margin-bottom:15px;background-color: #ea901f;border-color: #ea901f;" @click="addTag">新增</Button>
            <Button type="success" long style="margin-bottom:15px;background-color: #ea901f;border-color: #ea901f;" @click="replaceTag">替换</Button>
            <Button long @click="logout">退出登录</Button>
        </div>
    </div>
</template>
<script>
import util from '../../util/util'
export default {
    data(){
        return {
            device:'tag',
            Title:'选择要执行的操作',
            Uid:'',
            tagStatus:'0',
            detail:'该测温标签未注册使用'
        }
    },
    created(){
        let uuid = location.href.split('#')[0];
        uuid = uuid.split('&uid=')[1];
        this.Uid = uuid;
        let deviceType = location.href.split('#')[0];
        deviceType = deviceType.split('device=')[1].split('&')[0];

        if(deviceType=='device'){
            this.CheckTagInfo('device');
        }else if(deviceType=='reader'){
            this.CheckTagInfo('gateway');
        }

        if(sessionStorage.getItem('device') && deviceType==sessionStorage.getItem('device')){
            this.$router.push({name:'replaceTag',params:{device:this.device}});
        }
    },
    methods:{
        logout:function(){
            this.$router.push({name:'login'});
        },
        addTag:function(){
            this.$router.push({name:'addTag',params:{device:this.device}});
        },
        replaceTag:function(){
            this.$router.push({name:'replaceTag',params:{device:this.device}});
        },
        CheckTagInfo:function(type){
            let vm = this;
            let url = '/api-device/0/?uid='+vm.Uid;
            if(type=='gateway'){
                url = '/api-reader/0/?uid='+vm.Uid;
            }
            util.ajax({
                method: 'get',
                url: url
            })
            .then(function (response) {
                if(response.status==200){
                    vm.tagStatus = response.data.status;
                    vm.$store.commit('updateTagRegisterStatus',response.data.status);
                    vm.SetDetail();

                    let item = {
                        id:response.data.id,
                        tsname:response.data.tsname,
                        tname:response.data.tname,
                        group_name:response.data.group_name,
                        name:response.data.name,
                        did:response.data.did?response.data.did:response.data.rid,
                        uid:response.data.uid,
                        pic_path:response.data.pic_path
                    }
                    vm.$store.commit('updateAddTagInfo',item);
                }else{
                    vm.$Message.error('获取设备状态失败');
                }
            })
            .catch(function(err){
                if(err=='Error: Request failed with status code 404'){
                    vm.tagStatus = '0';
                    vm.SetDetail();
                    vm.$store.commit('updateTagRegisterStatus',0);
                }
            })
            
        },
        SetDetail:function(){
            let vm = this;
            let deviceType = location.href.split('#')[0];
            deviceType = deviceType.split('device=')[1].split('&')[0];
            if(deviceType=='device'){
                if(vm.tagStatus == '1'){
                    vm.detail = '该测温标签已注册使用';
                } 
                if(vm.tagStatus == '0'){
                    vm.detail = '该测温标签未注册使用';
                }
                vm.device = 'tag';
            }else if(deviceType=='reader'){
                console.log(vm.tagStatus)
                if(vm.tagStatus == '1'){
                    vm.detail = '该网关已注册使用';
                } 
                if(vm.tagStatus == '0'){
                    vm.detail = '该网关未注册使用';
                }
                vm.device = 'gateway';
            }else{
                vm.$Message.error("请扫描标签或者网关进行操作");
                return false;
            }
        }
    }
}
</script>
<style scoped>
    .phonebackbord{
        background: #ffffff;
        height: 100%;
        padding: 50px 15px;
    }
    .title{
        line-height: 50px;
        font-size: 18px;
        text-align: center;
    }
    .Uid{
        line-height: 50px;
        font-size: 17px;
        text-align: center;
    }
    .detail{
        line-height: 50px;
        font-size: 16px;
        text-align: center;
    }
    .btn_group{
        width: 100%;
        padding: 10px 10%;
    }
</style>
